﻿using System;
using System.ComponentModel;
using System.Text;
using FFmpegVideoClip.Configuration;

namespace FFmpegVideoClip.Lib
{
    public class LogManage:INotifyPropertyChanged
    {
        public static LogManage Instance { get; private set; }= new LogManage();

        public event PropertyChangedEventHandler? PropertyChanged;
        private StringBuilder m_Cache = new StringBuilder(5000);
        public StringBuilder Cache => m_Cache;

        //private void NotifyCacheChanged()
        //{
        //    if (App.Current.Dispatcher.CheckAccess()) //检查当前是否在UI线程
        //        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Cache)));
        //    else
        //        App.Current.Dispatcher.BeginInvoke(() => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Cache))));
        //    //PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Cache)));
        //}
        private void NotifyCacheChanged() => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Cache)));
        private void internalClear() { Cache.Clear(); NotifyCacheChanged(); }
        private void internalWriteNewInfo(string text) { if (string.IsNullOrEmpty(text)) return; Cache.Append(text); NotifyCacheChanged(); }
        private void internalWriteLineNewInfo(string text) { if (string.IsNullOrEmpty(text)) return; Cache.AppendLine(text); NotifyCacheChanged(); }
        private void internalWriteExceptionInfo(string OwnerInfo, Exception? exception) 
        {
            if (string.IsNullOrEmpty(OwnerInfo) && exception == null)
                return;
            Cache.AppendLine(OwnerInfo);

            if (exception is AggregateException ae)
            {
                foreach (Exception ex in ae.InnerExceptions)
                {
                    while (ex != null)
                    {
                        Cache.AppendLine(ex.Message);
                        Cache.AppendLine(ex.StackTrace);
                        exception = ex.InnerException;
                    }
                }
                NotifyCacheChanged();
                return;
            }

            while (exception != null)
            {
                Cache.AppendLine(exception.Message);
                Cache.AppendLine(exception.StackTrace);
                exception = exception.InnerException;
            }
            NotifyCacheChanged();
        }
        public static void Clear() => Instance.internalClear();
        public static void WriteNewInfo(string text,bool ForceWrite=false) { if (ForceWrite||Config.IsEnableWriteLog) Instance.internalWriteNewInfo(text); }
        public static void WriteLineNewInfo(string text, bool ForceWrite = false) { if (ForceWrite || Config.IsEnableWriteLog) Instance.internalWriteLineNewInfo(text); }
        public static void WriteExceptionInfo(string OwnerInfo, Exception exception, bool ForceWrite = true) { if (ForceWrite || Config.IsEnableWriteLog) Instance.internalWriteExceptionInfo(OwnerInfo, exception); }
    }
}
